<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>医生信息管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">医生姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="doctorName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">院区</label>
                            <div class="layui-input-inline">
                                <select name="areaId" id="searchArea">
                                    <option value="">请选择院区</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">科室</label>
                            <div class="layui-input-inline">
                                <select name="deptId" id="searchDept" lay-filter="areaSelect">
                                    <option value="">请选择科室</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

        <!-- 添加/编辑弹窗 -->
        <script type="text/html" id="doctorEditForm">
            <form class="layui-form" style="padding: 20px;" lay-filter="doctorForm">
                <input type="hidden" name="doctorId">
                <div class="layui-form-item">
                    <label class="layui-form-label">医生姓名</label>
                    <div class="layui-input-block">
                        <input type="text" name="doctorName" placeholder="请输入医生姓名" class="layui-input" lay-verify="required">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">医生头像</label>
                    <div class="layui-input-block">
                        <input type="text" name="doctorAvatar" placeholder="请输入头像URL" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">所属院区</label>
                    <div class="layui-input-block">
                        <select name="areaId" id="editArea" lay-filter="areaSelect">
                            <option value="">请选择院区</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">所属科室</label>
                    <div class="layui-input-block">
                        <select name="deptId" id="editDept">
                            <option value="">请选择科室</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">职称</label>
                    <div class="layui-input-block">
                        <select name="doctorTitle" lay-verify="required">
                            <option value="">请选择职称</option>
                            <option value="1">主任医师</option>
                            <option value="2">副主任医师</option>
                            <option value="3">主治医师</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">挂号费</label>
                    <div class="layui-input-block">
                        <input type="text" name="doctorRegistrationFee" placeholder="请输入挂号费" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">诊费</label>
                    <div class="layui-input-block">
                        <input type="text" name="doctorConsultationFee" placeholder="请输入诊费" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">医生简介</label>
                    <div class="layui-input-block">
                        <textarea name="doctorIntroduction" placeholder="请输入医生简介" class="layui-textarea"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="saveDoctor">保存</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </script>

    </div>
</div>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'upload'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer=layui.layer;

        // 初始化表格
        table.render({
            elem: '#currentTableId',
            url: '/doctor/page', // 后端查询接口
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'doctorName', width: 120, title: '医生姓名'},
                {field: 'doctorAvatar', width: 100, title: '医生头像', templet: function(d){
                        return '<div onclick="showImage(\''+d.doctorAvatar+'\')" style="cursor: pointer;"><img src="'+d.doctorAvatar+'" style="width: 30px; height: 30px; border-radius: 50%;"></div>';
                    }},
                {field: 'areaName', width: 150, title: '院区'},
                {field: 'deptName', width: 150, title: '科室'},
                {field: 'doctorTitle', width: 120, title: '职称', templet: function(d){
                        var titles = {1: '主任医师', 2: '副主任医师', 3: '主治医师'};
                        return titles[d.doctorTitle] || '未知';
                    }},
                {field: 'doctorIntroduction', title: '医生简介'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        // 页面加载时初始化下拉框数据
        $(document).ready(function() {
            loadAreaData('#searchArea');
            loadAreaData('#editArea');
           // loadDeptData('#searchDept');
            //loadDeptData('#editDept');
        });

        // 加载院区数据
        function loadAreaData(selector) {
            $.get('/area/list', function(res) {
                if(res.code == 0) {
                    var html = '<option value="">请选择院区</option>';
                    $.each(res.data, function(index, item) {
                        html += '<option value="'+item.areaId+'">'+item.areaName+'</option>';
                    });
                    $(selector).html(html);
                    form.render('select');
                }
            });
        }

        // 加载科室数据
        function loadDeptData(selector) {
            $.get('/dept/list', function(res) {
                if(res.code == 0) {
                    var html = '<option value="">请选择科室</option>';
                    $.each(res.data, function(index, item) {
                        html += '<option value="'+item.deptId+'">'+item.deptName+'</option>';
                    });
                    $(selector).html(html);
                    form.render('select');
                }
            });
        }

        // 监听院区选择事件，动态加载科室
        form.on('select(areaSelect)', function(data) {
            var areaId = data.value;
            if(areaId) {
                $.get('/dept/listByArea/' + areaId, function(res) {
                    if(res.code == 0) {
                        var html = '<option value="">请选择科室</option>';
                        $.each(res.data, function(index, item) {
                            html += '<option value="'+item.deptId+'">'+item.deptName+'</option>';
                        });
                        $('#editDept').html(html);
                        form.render('select');
                    }
                });
            } else {
                loadDeptData('#editDept');
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            // 执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                },
                where: {
                    doctorName: data.field.doctorName,
                    areaId: data.field.areaId,
                    deptId: data.field.deptId
                }
            });
            return false;
        });

        // toolbar监听事件
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                openDoctorEditDialog(null, '添加医生');
            }
        });

        // 监听表格工具栏事件
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                openDoctorEditDialog(data, '编辑医生');
            } else if (obj.event === 'delete') {
                deleteDoctor(data.doctorId, obj);
            }
        });

        var index;
        // 打开添加/编辑对话框
        function openDoctorEditDialog(data, title) {
            index= layer.open({
                title: title,
                type: 1,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['600px', '700px'],
                content: $('#doctorEditForm').html(),
                success: function(layero, index) {
                    if (!data) {
                        // 新增模式 - 重新加载下拉框
                        loadAreaData('#editArea');
                        loadDeptData('#editDept');
                    } else {
                        // 编辑模式 - 填充表单数据
                        form.val("doctorForm", data);

                        // 动态加载科室列表
                        var areaId = data.areaId;
                        if(areaId) {
                            $.get('/dept/listByArea/' + areaId, function(res) {
                                if(res.code == 0) {
                                    var html = '<option value="">请选择科室</option>';
                                    $.each(res.data, function(index, item) {
                                        var selected = item.deptId == data.deptId ? 'selected' : '';
                                        html += '<option value="'+item.deptId+'" '+selected+'>'+item.deptName+'</option>';
                                    });
                                    $('#editDept').html(html);
                                    form.render('select');
                                }
                            });
                        }
                    }

                    // 监听表单提交
                    form.on('submit(saveDoctor)', function(formdata) {
                        saveDoctor(formdata.field);
                        return false;
                    });
                }
            });
        }

        // 保存医生信息
        function saveDoctor(data) {
            var url = data.doctorId ? '/doctor/update' : '/doctor/add';
            var method = data.doctorId ? 'PUT' : 'POST';
            $.ajax({
                url: url,
                type: method,
                contentType: 'application/json',
                data: JSON.stringify(data),
                success: function(res) {
                    if(res.code == 0) {
                        layer.msg(res.msg, {icon: 1});
                        layer.close(index);
                        table.reload('currentTableId');
                    }else{
                        layer.msg(res.msg, {icon: 2});
                    }
                },
                error: function() {
                    layer.msg('操作失败', {icon: 2});
                }
            });
        }

        // 删除医生
        function deleteDoctor(doctorId, obj) {
            layer.confirm('确定要删除这个医生吗？', function(index) {
                $.ajax({
                    url: '/doctor/delete/' + doctorId,
                    type: 'DELETE',
                    success: function(res) {
                        if(res.code == 0) {
                            layer.msg(res.msg, {icon: 1});
                            layer.close(index);
                            table.reload('currentTableId');
                        }else{
                            layer.msg(res.msg, {icon: 2});
                        }
                    },
                    error: function() {
                        layer.msg('删除失败', {icon: 2});
                    }
                });
                layer.close(index);
            });
        }

        // 显示头像大图
        window.showImage = function(src) {
            if(src) {
                layer.open({
                    type: 1,
                    title: false,
                    closeBtn: 0,
                    area: '300px;',
                    shadeClose: true,
                    content: "<img src='"+src+"' style='width:100%;'/>"
                });
            }
        }

    });
</script>

</body>
</html>
